<?php


/*中间件文件*/
namespace app\http\middleware;

use app\index\controller\CheckAll;
use think\facade\Session;
use think\Request;

class Check
{
    public function handle(Request $request, \Closure $next)
    {
        $member = new \app\member\controller\db\Member();
        $check = new CheckAll();
        /*-----登录验证-----*/
        $checkFlag = $check->checkLogin($request->post("account"),$request->post("password"),$request);
        if($checkFlag){
            /*前置中间件 登录跳转*/
            if($checkFlag == "1"){
                /*管理员用户*/
                Session::set('login',$request->post("account"));
                return  redirect("/admin");
            }else if($checkFlag == "2"){
                /*普通用户界面*/
                if(filter_var($request->post("account"), FILTER_VALIDATE_EMAIL)){
                    $session_name = $member-> selectByEmail($request->post("account"))['member_name'];
                }else{
                    $session_name = $member->selectByName($request->post("account"))['member_name'];
                }
                Session::set('login',$session_name);
                return redirect("/user");
            }else if($checkFlag == "-1"){
                /*验证码错误*/
                return redirect("/login/"."1");
            }
        }else{
            /*密码错误*/
            return redirect("/login/"."2");
        }
        return $next($request);
    }
}
